// 二维码和登录相关业务逻辑模块
const { v4: uuidv4 } = require('uuid')
const QRCode = require('qrcode')
const axios = require('axios')

// 微信小程序配置（请替换为你自己的）
const wxConfig = {
  appId: 'YOUR_APPID',
  appSecret: 'YOUR_APPSECRET'
}

// 存储二维码状态的Map
const qrCodeMap = new Map()

// 示例数据（模拟数据表）
const sampleData = [
  { name: '项目A', value: 100, date: new Date('2023-01-01') },
  { name: '项目B', value: 200, date: new Date('2023-01-02') },
  { name: '项目C', value: 150, date: new Date('2023-01-03') },
  { name: '项目D', value: 300, date: new Date('2023-01-04') },
  { name: '项目E', value: 250, date: new Date('2023-01-05') }
]

// 微信小程序登录，换取openid
async function wxLogin(code, logMessage) {
  try {
    const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${wxConfig.appId}&secret=${wxConfig.appSecret}&js_code=${code}&grant_type=authorization_code`
    const response = await axios.get(url)
    if (response.data.errcode) {
      throw new Error(`微信登录失败: ${response.data.errmsg}`)
    }
    return { openid: response.data.openid, session_key: response.data.session_key }
  } catch (error) {
    logMessage && logMessage('微信登录接口调用失败', { error: error.message })
    throw error
  }
}

module.exports = {
  wxConfig,
  qrCodeMap,
  sampleData,
  wxLogin,
  QRCode,
  uuidv4
} 